<?php
/**
 *	Views of Messages.
 *	@package		tools.BugTracker
 *	@author			Christian Würker <Christian.Wuerker@CeuS-Media.de>
 *	@since			0.1
 *	@version		$Id$
 */
/**
 *	Views of Messages.
 *	@package		tools.BugTracker
 *	@extends		CMF_Krypton_Core_DefinitionView
 *	@uses			Message
 *	@uses			User
 *	@author			Christian Würker <Christian.Wuerker@CeuS-Media.de>
 *	@since			0.1
 *	@version		$Id$
 *	@todo			Language Support of Messages
 *	@todo			Code Documentation
 */
class View_Message extends CMF_Krypton_Core_DefinitionView
{
	public static $linkId	= 'message';

	public function __construct()
	{
		parent::__construct( TRUE );
		$this->request	= $this->registry->get( 'request' );
		$this->auth		= $this->registry->get( 'auth' );
	}

	protected function getUrl( $parameters = array() )
	{
		$param	= http_build_query( $parameters, NULL, '&' );
		$param	= $param ? '&'.$param : '';
		$url	= './?link='.self::$linkId.$param;
		return $url;
	}

	/**
	 *	Builds Content Views.
	 *	@access		public
	 *	@return		void
	 */
	public function buildContent()
	{
		$current	= $this->auth->getCurrentUser();
		
		$messageId	= $this->request->get( 'messageId' );
		if( $messageId )
		{
			switch( $this->request->get( 'action' ) )
			{
				case 'reply':
					$view		= new View_Message_Reply();
					$content	= $view->buildContent();#
					break;
				case 'forward':
					$view		= new View_Message_Forward();
					$content	.= $view->buildContent();
					break;
				default:
					$view		= new View_Message_View();
					$content	= $view->buildContent();
					break;
			}
		}
		else
		{
			switch( $this->request->get( 'action' ) )
			{
				case 'add':
					$view		= new View_Message_Send();
					$content	= $view->buildContent();
					break;
				default:
					$view		= new View_Message_List();
					$content	= $view->buildContent();
					break;
			}

		}
		return $content;
	}

	/**
	 *	Builds Control View from Wiki File.
	 *	@access		public
	 *	@return		string
	 */
	public function buildExtra()
	{
		return $this->loadContent( "message_control.wiki" );
	}

	protected function getBodyCite( $message, $string )
	{
		$sender		= Logic_User::getUserData( $message['source'] );								// get user data of message sender
		$format		= $this->config['layout.format.timestamp'];										// get date time format from config
		$datetime	= date( $format, $message['timestamp'] );										// format date of message
		$header		= sprintf( $string, $sender['username'], $datetime );					// fill header line with sender and date
		$bodyCopy	= str_replace( "\n", "\n> ", $message['body'] );								// cite body
		return		$header."\n> ".$bodyCopy;														// prepend header line to cited body
	}

	/**
	 *	...
	 *	@access		protected
	 *	@return		string
	 */
	protected function getStatusIcon( $status )
	{
		$config	= $this->registry->get( 'config' );
		$path	= $config['paths']['themes'].$config['layout']['theme'].'/';
		$url	= $path.'images/icons/message'.$status.'.png';
		$label	= $this->words['message_states'][$status];
		$icon	= $this->html->Image( $url, $label );
		return $icon;
	}

	/**
	 *	...
	 *	@access		protected
	 *	@param		...
	 *	@return		string
	 */
	protected function includeBugLink( $string )
	{
		if( preg_match( "@bug ?#[0-9]@i", $string ) )
			$string	= preg_replace_callback( "@bug ?#([0-9]+)@i", array( $this, 'includeBugLink_callback' ), $string );
		return $string;
	}

	/**
	 *	...
	 *	@access		protected
	 *	@param		...
	 *	@return		string
	 */
	protected function includeBugLink_callback( $matches )
	{
		$link		= $this->html->Link( "?link=bugs&bug_id=".$matches[1], "Bug #".$matches[1] );
		return $link;
	}
}
?>